// pages/wechatpaty/wechatpaty.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    paymony:'',
         // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示 0表示不显示
      title: '余额支付', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      paymony:options.paymony
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  inputPassword(e) {
    let that = this;
    //键盘输入的密码 赋值给inputPassword
    let inputPassword = that.data.inputPassword + e.currentTarget.dataset.key;
    that.setData({
      inputPassword: inputPassword
    })
    console.log(that.data.inputPassword)
    //当输入密码完成时 提交密码并给个提示
    if (that.data.inputPassword.length == 6) {
      wx.showLoading({
        title: '支付中',
      })
      let url = app.globalData.URL + 'facility/printOrder/pay?id=' + that.data.payid + '&payPassword=' + that.data.inputPassword;
      let data = {}
      app.wxRequest('POST', url, data, (res1) => {
        wx.hideLoading();
        if (res1.code == 200) {
          // 支付成功
          that.passwordInputHidden();
          wx.showModal({
            title: '支付成功',
            icon: 'none'
          })
          wx.switchTab({
            url: '/pages/index/index'
          });
        }else if (res1.code == 500) {
          //支付密码错误
          wx.showModal({
            title: "提示",
            content: "余额不足",
          })
          that.setData({
            inputPassword: ''
          });
        } else if (res1.code == 502) {
          //支付密码错误
          wx.showModal({
            title: "提示",
            content: "输入密码错误",
          })
          that.setData({
            inputPassword: ''
          });
        } else if (res1.code == 501) {
          // 未设置支付密码
          wx.showModal({
            title: "提示",
            content: "未设置支付密码",
          })
          that.passwordInputHidden()
          wx.navigateTo({
            url: '/pages/my/setPassword/setPassword'
          })
        }
      }, (err) => {
        console.log(err.errMsg)
      })
    }

  },
  passwordInputHidden() {
    this.setData({
      passwordInputHidden: !this.data.passwordInputHidden //取反 打开关闭小键盘
    });
    this.setData({
      inputPassword: ''
    });
  },
  //删除输入错误的密码
  clear() {
    var index = this.data.inputPassword.length;
    if (index > 0) {
      var inputPassword = this.data.inputPassword.substr(0, index - 1);
      this.setData({
        inputPassword: inputPassword
      });
    }
  },
  mycoupon: function () {
    wx.navigateTo({
      url: '/pages/my/my_coupon/my_coupon',
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },
  gopaybalance:function(){
      let that = this;
      let content = that.data.pics;
      let count = that.data.num;
      let facilityCode = that.data.facilityCode;
      let isColours = that.data.isColours;
      let printType = that.data.printType;
      let title = that.data.title;
      let type = that.data.type;
      let typeCode = that.data.typeCode;
      let url = app.globalData.URL + 'facility/printOrder?';
      let data = {};
      wx.request({
        url: url + 'facilityCode=' + that.data.facilityCode + '&content=' + content + '&count=' + count +
          '&isColours=' + isColours + '&printType=' + that.data.printType + '&title=' + title + '&type=' + type + '&typeCode=' + that.data.typeCode + '&payType=0',
        method: 'POST',
        header: {
          // 'content-type': 'application/json',
          'Authorization': getApp().globalData.token // 默认值
        },
        success: function (res) {
          that.setData({
            payid: res.data.data.id
          })
          let pay_type = that.data.pay_type;
            // 余额支付
            that.passwordInputHidden();
          console.log(res)
        }
      })
  }
  
})